/**
 * Copyright (c) 2018, Alex. All rights reserved.
 */
package com.oschina.bottle.coder.converter;

import java.util.Base64;
import java.util.Base64.Decoder;
import java.util.Base64.Encoder;

/**
 * 简化Base64的调用
 *
 * Base64算法中，有两种字符集。可以使得字符串 和 byte[] 数组互相转换
 *
 * @author <a href="mailto:zhongchao@gegejia.com">zhong</a>
 * @version 1.0 2018/1/9
 * @since 1.0
 */
public class Base64Coverter {

    private boolean useUrl;

    public Base64Coverter() {
    }

    public Base64Coverter(boolean useUrl) {
        this.useUrl = useUrl;
    }


    public String encodeToString(byte[] data) {
        return getEncoder().encodeToString(data);
    }

    public byte[] decodeToByte(String data) {
        return getDecoder().decode(data);
    }

    private Encoder getEncoder() {
        return useUrl ? Base64.getUrlEncoder() : Base64.getEncoder();
    }

    private Decoder getDecoder() {
        return useUrl ? Base64.getUrlDecoder() : Base64.getDecoder();
    }
}
